/**
 * @Title: RemoteHistoryAdapter.java
 * @author 6335yz
 * @date 2015年8月20日 上午10:49:59
 */
package com.shanghaionstar.user.adapter;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.base.CommonAdapter;
import com.onstar.cn.framework.base.CommonViewHolder;
import com.onstar.cn.mag.enumtype.RemoteCommandsType;
import com.onstar.cn.vehicle.entity.RequestStatus;
import com.onstar.cn.vehicle.response.CommandResponseEntity;
import com.onstar.cn.weight.CustomImageView;
import com.shanghaionstar.R;
import com.shanghaionstar.utils.DateFormatUtil;

import java.util.List;

/**
 * @param
 * @author 6335yz
 * @ClassName: RemoteHistoryAdapter
 * @Description: TODO
 * @date 2015年8月20日 上午10:49:59
 */
public class RemoteHistoryAdapter extends CommonAdapter<CommandResponseEntity> {

    private List<CommandResponseEntity> commandResponses;
    private Context context;

    /**
     * @Description: TODO
     * @author 6335yz
     * @date 2015年8月20日 上午10:51:57
     */
    public RemoteHistoryAdapter(List<CommandResponseEntity> datas, Context context) {
        super(datas, context);
        this.commandResponses = datas;
        this.context = context;
    }

    /**
     * (non-Javadoc)
     * <p>Title: getView</p>
     * <p>Description: </p>
     *
     * @param position
     * @param viewHolder
     * @see com.onstar.cn.framework.base.CommonAdapter#getView(int, com.onstar.cn.framework.base.CommonViewHolder)
     */
    @Override
    public void getView(int position, CommonViewHolder viewHolder) {
        ViewHolder holder = (ViewHolder) viewHolder;
        CommandResponseEntity responseEntity = commandResponses.get(position);
        String date = DateFormatUtil.format(context, responseEntity.getCompletionTime());
        holder.remote_control_info.setText(date);
        holder.remote_control_statues.setBackgroundResource(getStatuesResourceId(responseEntity.getStatus()));
        holder.remote_control_title.setText(getValueResourceId(responseEntity.getType()));
        if (responseEntity.getStatus().equals(RequestStatus.InProgress.getStringValue())) {
            holder.tv_remote_control_statues.setVisibility(View.GONE);
        } else if (responseEntity.getStatus().equals(RequestStatus.Success.getStringValue())) {
            holder.tv_remote_control_statues.setText(R.string.remote_control_success);
            holder.tv_remote_control_statues.setVisibility(View.VISIBLE);
        } else {
            holder.tv_remote_control_statues.setText(R.string.remote_control_failure);
            holder.tv_remote_control_statues.setVisibility(View.VISIBLE);
        }
    }

    /**
     * (non-Javadoc)
     * <p>Title: layoutId</p>
     * <p>Description: </p>
     *
     * @return
     * @see com.onstar.cn.framework.base.CommonAdapter#layoutId()
     */
    @Override
    public int layoutId() {
        return R.layout.onstar_new_remote_control_history;
    }

    /**
     * (non-Javadoc)
     * <p>Title: getViewHolder</p>
     * <p>Description: </p>
     *
     * @return
     * @see com.onstar.cn.framework.base.CommonAdapter#getViewHolder()
     */
    @Override
    public CommonViewHolder getViewHolder() {
        return new ViewHolder();
    }

    private class ViewHolder extends CommonViewHolder {
        @FindViewById
        TextView remote_control_info;
        @FindViewById
        TextView remote_control_title;
        @FindViewById
        TextView tv_remote_control_statues;
        @FindViewById
        CustomImageView remote_control_statues;
    }

    private int getIconResourceId(String requestTypeStr) {
        int id = 0;
        if (RemoteCommandsType.fromValue(requestTypeStr) == null) {
            return id;
        }
        RemoteCommandsType commandsType = RemoteCommandsType.fromValue(requestTypeStr);
        if (commandsType == null) return 0;
        switch (commandsType) {
            case COMMANDS_UNLOCK_DOOR:
                id = R.drawable.icon_rc_unlock;
                break;

            case COMMANDS_LOCK_DOOR:
                id = R.drawable.icon_rc_lock;
                break;

            case COMMANDS_ALERT:
                id = R.drawable.icon_rc_horn_light;
                break;

            case COMMANDS_START:
                id = R.drawable.icon_rc_remote_start;
                break;

            case COMMANDS_CANCEL_START:
                id = R.drawable.icon_rc_remote_start_cancel;
                break;
            case COMMANDS_DIAGNOSTICS:
                id = R.drawable.icon_rc_refresh;
                break;
            case COMMANDS_LOCATION:
                id = R.drawable.icon_rc_find_vehicle;
                break;
            case COMMANDS_TBT_ROUTE:
                id = R.drawable.send_car;
                break;
            case COMMANDS_NAV_DESTINATION:
                id = R.drawable.send_odd;
                break;
            case COMMAND_GETCHARGINGPROFILE:
                id = R.drawable.onstar_charge_mode;
                break;
            case COMMAND_SETCHARGINGPROFILE:
                id = R.drawable.onstar_charge_mode;
                break;
            case COMMAND_GETCOMMUTESCHEDULE:
                id = R.drawable.onstar_charge_time;
                break;
            case COMMAND_SETCOMMUTESCHEDULE:
                id = R.drawable.onstar_charge_time;
                break;
            case COMMAND_GETHOTSPOTINFO:
                id = R.drawable.wifi;
                break;
            case COMMAND_SETHOTSPOTINFO:
                id = R.drawable.wifi;
                break;
            default:
                break;
        }
        return id;
    }

    private int getStatuesResourceId(String requestStatuesStr) {
        RequestStatus requestStatus = RequestStatus.fromValue(requestStatuesStr);
        int id = 0;
        if (requestStatus == null) return id;
        switch (requestStatus) {
            case Failure:
                id = R.drawable.v_c_f;
                break;
            case InProgress:
                id = R.drawable.v_c_l;
                break;
            case Success:
                id = R.drawable.v_c_c;
                break;
            default:
                break;
        }
        return id;
    }

    private String getValueResourceId(String requestTypeStr) {
        int resId = 0;
        RemoteCommandsType requestType = RemoteCommandsType.fromValue(requestTypeStr);
        if (requestType == null) {
            return "";
        }
        switch (requestType) {
            case COMMANDS_UNLOCK_DOOR:
                resId = R.string.unlock;
                break;
            case COMMANDS_LOCK_DOOR:
                resId = R.string.lock;
                break;

            case COMMANDS_ALERT:
                resId = R.string.remote_alarm;
                break;

            case COMMANDS_START:
                resId = R.string.start;
                break;
            case COMMANDS_CANCEL_START:
                resId = R.string.stop;
                break;
            case COMMANDS_LOCATION:
                resId = R.string.find_vehicle;
                break;
            case COMMANDS_DIAGNOSTICS:
                resId = R.string.data_refresh;
                break;
            case COMMANDS_TBT_ROUTE:
                resId = R.string.pilot_audio;
                break;
            case COMMANDS_NAV_DESTINATION:
                resId = R.string.navigator_car;
                break;
            case COMMAND_GETCHARGINGPROFILE:
                resId = R.string.charge_get_profile;
                break;
            case COMMAND_SETCHARGINGPROFILE:
                resId = R.string.charge_set_profile;
                break;
            case COMMAND_GETCOMMUTESCHEDULE:
                resId = R.string.charge_get_Schedule;
                break;
            case COMMAND_SETCOMMUTESCHEDULE:
                resId = R.string.charge_set_Schedule;
                break;
            case COMMAND_GETHOTSPOTINFO:
                resId = R.string.wifi_fetch;
                break;
            case COMMAND_SETHOTSPOTINFO:
                resId = R.string.wifi_set;
                break;
            default:
                break;
        }
        if (resId != 0) {
            return context.getResources().getString(resId).trim();
        } else {
            return "";
        }
    }

//    class OnPreDrawListenerImp implements OnPreDrawListener {
//        AnimationDrawable frameAnimation;
//
//        public OnPreDrawListenerImp(Drawable drawable) {
//            this.frameAnimation = (AnimationDrawable) drawable;
//        }
//
//        @Override
//        public boolean onPreDraw() {
//            frameAnimation.start();
//            return true;
//        }
//    }
}
